<?xml version="1.0" encoding="windows-1251"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="../Dictionary.xslt"/>
  <xsl:import href="PrintSpec.xslt"/>

  <xsl:template match="/MiniEditor/Transforms"/>
  <xsl:template match="/MiniEditor/Lists"/>
  
  <xsl:variable name="RouteOKATO">
    <xsl:for-each select="//Forms/Form[@Code='Request']/*/*/*">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="'RouteOKATO'"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:variable>

  <xsl:template name="RouteOKATO">
    <xsl:comment>
      <xsl:value-of select="normalize-space($RouteOKATO)"/>
    </xsl:comment>
  </xsl:template>

  <!-- FormatCheckSettings -->
  <xsl:variable name="VersionRequest">
    <xsl:for-each select="//Forms/Form[@Code='Info']/*/*/*">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="'VersionRequest'"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="VersionMP">
    <xsl:for-each select="//Forms/Form[@Code='Info']/*/*/*">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="'VersionMP'"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="VersionTP">
    <xsl:for-each select="//Forms/Form[@Code='Info']/*/*/*">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="'VersionTP'"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="ProgramS">
    <xsl:for-each select="//Forms/Form[@Code='Info']/*/*/*">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="'ProgramS'"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="ProgramR">
    <xsl:for-each select="//Forms/Form[@Code='Info']/*/*/*">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="'ProgramR'"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="CodeReg" select="normalize-space(//Forms/@SetCodeReg)"/>
  <!--xsl:variable name="CodePack" select="normalize-space(//Forms/@SetCodePack)"/-->
  <xsl:variable name="Date" select="normalize-space(//Forms/@SetDate)"/>
  <xsl:variable name="Time" select="normalize-space(//Forms/@SetTime)"/>
  <xsl:variable name="PackID" select="normalize-space(//Forms/@SetID)"/>
  <xsl:variable name="GUID_Contractor">
    <xsl:choose>
      <xsl:when test="//Forms/@SetUID[.!='@']">
        <xsl:value-of select="concat('ID',translate(//Forms/@SetUID,'@.','_'))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='GUID_Contractor']"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="Method" select="normalize-space(//Form[@Code='MP']/Page[@Code='Common']/*/*[@Selected='true']/*[@Code='Method'])"/>
  <xsl:variable name="TitleRequest">
    <xsl:choose>
      <xsl:when test="//Form[@Code='MP']/Page[@Code='Common']/*/*[@Selected='true']/*[@Code='Request1'] and $Method='SP1'">
        <xsl:value-of select="normalize-space(//Form[@Code='MP']/Page[@Code='Common']/*/*[@Selected='true']/*[@Code='Request1']/*[@Selected='true']/@Code)"/>
      </xsl:when>
      <xsl:when test="$Method='SP1'">true</xsl:when>
      <xsl:otherwise>false</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="JFSender" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='JF'])"/>
  <xsl:variable name="MJFDeclarant" select="normalize-space(//Form[@Code='Request']/Page[@Code='Declarants']/*[@Code='Declarant']/*[@Selected='true']/@Code)"/>
  <xsl:variable name="CodeSender" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='CodeSender'])"/>
  <xsl:variable name="Organization" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Organization'])"/>
  <xsl:variable name="INNJ" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='INNJ'])"/>
  <xsl:variable name="INNF" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='INNF'])"/>
  <xsl:variable name="KPP" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='KPP'])"/>
  <xsl:variable name="OGRN" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='OGRN'])"/>
  <xsl:variable name="RegDate" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='RegDate'])"/>
  <xsl:variable name="Phone" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Phone'])"/>
  <xsl:variable name="Email" >
    <xsl:choose>
      <xsl:when test="//Forms/@SetUID[.!='@']">
        <xsl:value-of select="normalize-space(//Forms/@SetUID)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Email'])"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="Address" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Address'])"/>
  <xsl:variable name="FIO1" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='FIO1'])"/>
  <xsl:variable name="FIO2" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='FIO2'])"/>
  <xsl:variable name="FIO3" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='FIO3'])"/>
  <xsl:variable name="Appointment" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Appointment'])"/>
  <xsl:variable name="LetterNumber" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*[not(@Selected='false')]/*[@Code='LetterNumber'])"/>
  <xsl:variable name="LetterDate1" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*[not(@Selected='false')]/*[@Code='LetterDate1'])"/>
  <xsl:variable name="LetterDate2" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*[not(@Selected='false')]/*[@Code='LetterDate2'])"/>
  <xsl:variable name="CertN" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='CertN'])"/>
  <xsl:variable name="Type_Document" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Type_Document']/*[@Selected='true']/@Code)"/>
  <xsl:variable name="DocSeries" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Series'])"/>
  <xsl:variable name="DocNumber" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Number'])"/>
  <xsl:variable name="DocDate" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='Date'])"/>
  <xsl:variable name="DocIssueOrgan" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='IssueOrgan'])"/>
  <xsl:variable name="CodeRecipient" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='CodeRecipient'])"/>
  <xsl:variable name="NameRecipient" select="normalize-space(//Form[@Code='Info']/Page[@Code='Info']/*/*/*[@Code='NameRecipient'])"/>
  <xsl:variable name="CodeRequestDoc" select="normalize-space(//Form[@Code='Request']/Page[@Code='Request_Documents']/*[@Code='Request_Document']/*[1]/*[@Code='SubCode']/*[@Selected='true']/@Code)"/>

  <xsl:template name="EDocument">
    <xsl:param name="CodeDoc"/>
    <xsl:param name="VersionDoc"/>
    <eDocument>
      <xsl:attribute name="CodeType">
        <xsl:choose>
          <xsl:when test="$CodeDoc='Request' and $ProgramR=''">
            <xsl:text>107</xsl:text>
          </xsl:when>
          <xsl:when test="$CodeDoc='Request' and $ProgramR='Registrator'">
            <xsl:text>107</xsl:text>
          </xsl:when>
          <xsl:when test="$CodeDoc='Request' and $ProgramR='Portal'">
            <xsl:text>112</xsl:text>
          </xsl:when>
          <xsl:when test="$CodeDoc='MP' and $CodeReg='nwz'">
            <xsl:text>014</xsl:text>
          </xsl:when>
          <xsl:when test="$CodeDoc='MP' and $CodeReg='chz'">
            <xsl:text>015</xsl:text>
          </xsl:when>
        </xsl:choose>
      </xsl:attribute>
      <xsl:attribute name="Version">
        <xsl:choose>
          <xsl:when test="$CodeDoc='Request'">
            <xsl:choose>
              <xsl:when test="$VersionRequest=''">
                <xsl:text>12</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$VersionRequest"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:when test="$CodeDoc='MP'">
            <xsl:choose>
              <xsl:when test="$VersionMP=''">
                <xsl:text>01</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$VersionMP"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
        </xsl:choose>
      </xsl:attribute>
      <xsl:if test="$CodeDoc='MP'">
        <xsl:attribute name="GUID"/>
      </xsl:if>
      <Sender>
        <xsl:attribute name="Kod">
          <xsl:value-of select="$CodeSender"/>
        </xsl:attribute>
        <xsl:attribute name="Name">
          <xsl:choose>
            <xsl:when test="$JFSender='J'">
              <xsl:value-of select="$Organization"/>
            </xsl:when>
            <xsl:when test="$JFSender='F'">
              <xsl:value-of select="$FIO1"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="$FIO2"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="$FIO3"/>
            </xsl:when>
          </xsl:choose>
        </xsl:attribute>
        <xsl:if test="$CodeDoc='MP'">
          <xsl:attribute name="Date_Upload">
            <xsl:value-of select="$Date"/>
          </xsl:attribute>
        </xsl:if>
      </Sender>
      <Recipient>
        <xsl:attribute name="Kod">
          <xsl:value-of select="$CodeRecipient"/>
        </xsl:attribute>
        <xsl:attribute name="Name">
          <xsl:value-of select="$NameRecipient"/>
        </xsl:attribute>
      </Recipient>
      <xsl:if test="$CodeDoc='Request'">
        <xsl:call-template name="RouteOKATO"/>
      </xsl:if>
    </eDocument>
  </xsl:template>

  <xsl:template name="XmlFileName">
    <xsl:param name="CodeDoc"/>
    <xsl:variable name="Prefix">
      <xsl:choose>
        <xsl:when test="$CodeDoc = 'Request'">
          <xsl:text>RREQ</xsl:text>
          <!--xsl:choose>
            <xsl:when test="$ProgramR = 'Portal'">
              <xsl:text>req</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>RREQ</xsl:text>
            </xsl:otherwise>
          </xsl:choose-->
        </xsl:when>
        <xsl:when test="$CodeDoc = 'MP'">
          <xsl:text>GKUZU</xsl:text>
        </xsl:when>
        <xsl:when test="$CodeDoc = 'TP'">
          <xsl:text>TUOKS</xsl:text>
        </xsl:when>
        <xsl:when test="$CodeDoc = 'Application'">
          <xsl:text>APP</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>XXX</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="$Prefix"/>
    <xsl:text>_</xsl:text>
    <!--xsl:if test="not($CodePack='' or $CodePack='startA')">
      <xsl:for-each select="//Form[@Code='Request']/Page[@Code='Declarants']/*[@Code='Reg_Folder']/*">
        <xsl:call-template name="Value">
          <xsl:with-param name="x" select="'SessionID'"/>
        </xsl:call-template>
      </xsl:for-each>
    </xsl:if-->
    <xsl:text>.xml</xsl:text>
  </xsl:template>

  <xsl:template name="MainElement">
    <xsl:param name="CodeDoc"/>
    <xsl:choose>
      <xsl:when test="$CodeDoc = 'Request'">
        <xsl:text>Requests_GZK_Realty</xsl:text>
      </xsl:when>
      <xsl:when test="$CodeDoc = 'MP'">
        <xsl:text>STD_MP</xsl:text>
      </xsl:when>
      <xsl:when test="$CodeDoc = 'TP'">
        <xsl:text>Register_Realty</xsl:text>
      </xsl:when>
      <xsl:when test="$CodeDoc = 'Application'">
        <xsl:text>Applied_Documents</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>XXX</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="AddFilesDescription">
    <xsl:param name="CodeDoc"/>
    <AddFiles>
      <xsl:attribute name="FolderName">
        <xsl:call-template name="MainElement">
          <xsl:with-param name="CodeDoc" select="$CodeDoc"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:for-each select="../Form[@Selected='true']/Page/*[@Selected='true']/*[not(@Selected='false')]/ParamFile[.!='' and contains(@Code,$CodeDoc)]">
        <AddFile>
          <xsl:attribute name="Path">
            <xsl:value-of select="."/>
          </xsl:attribute>
          <xsl:attribute name="FileName">
            <xsl:call-template name="OurFilePrefix">
              <xsl:with-param name="CodeDoc" select="$CodeDoc"/>
            </xsl:call-template>
            <xsl:value-of select="position()"/>
            <xsl:if test="@SetType!=''">
              <xsl:text>.</xsl:text>
              <xsl:value-of select="@SetType"/>
            </xsl:if>
          </xsl:attribute>
        </AddFile>
      </xsl:for-each>
    </AddFiles>
  </xsl:template>

  <xsl:template name="AddFileReference">
    <xsl:param name="CodeDoc"/>
    <xsl:variable name="ID" select="@SetID"/>
    <xsl:variable name="Type" select="@SetType"/>
    <xsl:call-template name="MainElement">
      <xsl:with-param name="CodeDoc" select="$CodeDoc"/>
    </xsl:call-template>
    <xsl:text>\</xsl:text>
    <xsl:call-template name="OurFilePrefix">
      <xsl:with-param name="CodeDoc" select="$CodeDoc"/>
    </xsl:call-template>
    <xsl:for-each select="//Form[@Selected='true']/Page/*[@Selected='true']/*[not(@Selected='false')]/ParamFile[.!='' and contains(@Code,$CodeDoc)]">
      <xsl:if test="@SetID = $ID">
        <xsl:value-of select="position()"/>
      </xsl:if>
    </xsl:for-each>
    <xsl:if test="$Type!=''">
      <xsl:text>.</xsl:text>
      <xsl:value-of select="$Type"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="Images">
    <xsl:param name="CodeDoc"/>
    <xsl:if test="ParamFile[.!='']">
      <Images>
        <xsl:for-each select="ParamFile[.!='']">
          <Image>
            <xsl:attribute name="Name">
              <xsl:call-template name="AddFileReference">
                <xsl:with-param name="CodeDoc" select="$CodeDoc"/>
              </xsl:call-template>
            </xsl:attribute>
          </Image>
        </xsl:for-each>
      </Images>
    </xsl:if>
  </xsl:template>

  <xsl:template name="AppliedFiles">
    <xsl:param name="CodeDoc"/>
    <xsl:param name="CodeType"/>
    <xsl:if test="ParamFile[.!='']">
      <AppliedFiles>
        <xsl:for-each select="ParamFile[.!='']">
          <AppliedFile>
            <xsl:attribute name="type">
              <xsl:value-of select="$CodeType"/>
            </xsl:attribute>
            <xsl:attribute name="name">
              <xsl:call-template name="AddFileReference">
                <xsl:with-param name="CodeDoc" select="$CodeDoc"/>
              </xsl:call-template>
            </xsl:attribute>
          </AppliedFile>
        </xsl:for-each>
      </AppliedFiles>
    </xsl:if>
  </xsl:template>

  <xsl:template name="OurFilePrefix">
    <xsl:param name="CodeDoc"/>
    <xsl:choose>
      <xsl:when test="$CodeDoc='Request'">
        <xsl:text>RFile</xsl:text>
      </xsl:when>
      <xsl:when test="$CodeDoc='MP'">
        <xsl:text>MFile</xsl:text>
      </xsl:when>
      <xsl:when test="$CodeDoc='TP'">
        <xsl:text>TFile</xsl:text>
      </xsl:when>
      <xsl:when test="$CodeDoc='Application'">
        <xsl:text>ApplicationFile</xsl:text>
      </xsl:when>
      <xsl:otherwise>XXX</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="Value">
    <xsl:param name="x"/>
    <xsl:param name="list"/>
    <xsl:param name="this"/>
    <xsl:choose>
      <xsl:when test="*[@ListName=$list]">
        <xsl:variable name="id" select="*[@ListName=$list]/Item/@id"/>
        <xsl:for-each select="../../../../../Lists/List[@Name=$list]/Items/Item[@id=$id]">
          <xsl:choose>
            <xsl:when test="*[@Code=$x]/*[@Selected='true']/@Code">
              <xsl:value-of select="normalize-space(*[@Code=$x]/*[@Selected='true']/@Code)"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="normalize-space(*[@Code=$x])"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$this='true'">
            <xsl:value-of select="normalize-space(.)"/>
          </xsl:when>
          <xsl:when test="*[@Code=$x]/*[@Selected='true']/@Code">
            <xsl:value-of select="normalize-space(*[@Code=$x]/*[@Selected='true']/@Code)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="normalize-space(*[@Code=$x])"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="Value-">
    <xsl:param name="x"/>
    <xsl:param name="list"/>
    <xsl:param name="pos"/>
    <xsl:variable name="V">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="$x"/>
        <xsl:with-param name="list" select="$list"/>
        <xsl:with-param name="pos" select="$pos"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$V!=''">
        <xsl:value-of select="$V"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="Space" />
        <xsl:text>-</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="Dicts">
    <xsl:param name="dict"/>
    <xsl:param name="code"/>
    <xsl:choose>
      <xsl:when test="$dict!='' and $code!=''">
        <xsl:variable name="BcodeB">
          <xsl:text>|</xsl:text>
          <xsl:value-of select="$code"/>
          <xsl:text>|</xsl:text>
        </xsl:variable>
        <xsl:value-of select="substring-before(substring-after($dict,$BcodeB),'|')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>-</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
